package se.liu.oschi129.animation;

import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

/**
 * This class can be used to create animated images. Add one or many Images
 * to the Animation and use its methods to return the subimages used in the animation.
 * 
 * @author oschi129
 */
public class Animation {
	
	// This is a list of all BufferedImages in the animation
	private final List<Image> animation = new ArrayList<Image>();
	
	// The size of the animation (number of subimages)
	private int size;
	
	public Animation() {
		size = 0;
	}
	
	/** Adds an image to the BufferedAnimation */
	public void addImage(Image image) {
		size++;
		animation.add(image);
	}
	
	/** Adds an image from a jar-resource */
	public void addImageFromResource(String name) throws AnimationIOException {
		try {
			addImage(ImageIO.read(new File(name)));
		} catch (IOException exception) {
			throw new AnimationIOException(name);
		}
	}
	
	/** Removes an subimage from the BufferedAnimation */
	public void removeImage(int subimage) {
		animation.remove(size - subimage - 1);
		size--;
	}
	
	/** Returns the BufferedImage from a subimage in BufferedAnimation */
	public Image subimage(int subimage) {
		return animation.get(size - subimage - 1);
	}
	
	/** Returns the number of subimages in the BufferedAnimation */
	public int length() {
		return size;
	}
}
